package com.holyn.guofang.view.distinguish;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.viewcommon.multileveltreelist.Node;

import java.util.List;

/**
 * Created by Holyn on 2017/7/25.
 */

public class ImageElementSearchRecyclerAdapter extends BaseQuickAdapter<Node, BaseViewHolder> {
    private boolean isOnBind = false;

    public ImageElementSearchRecyclerAdapter(@Nullable List<Node> data) {
        super(R.layout.item_image_element_search, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, Node node) {
        isOnBind = true;
        CheckBox cbImageCode = baseViewHolder.getView(R.id.cb_image_code);
        cbImageCode.setChecked(node.isChecked());
        cbImageCode.setText(node.getId()+"");
        baseViewHolder.setText(R.id.tv_image_name, node.getName().replace(node.getId()+" ", ""));
        isOnBind = false;
        cbImageCode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isOnBind){
                    getData().get(baseViewHolder.getAdapterPosition() - getHeaderLayoutCount()).setChecked(isChecked);
                    notifyDataSetChanged();
                }
            }
        });

    }

    public String getSelectImageCodes(){
        String result = "";
        int selectCount = 0;
        for (Node node : getData()){
            if (node.isChecked()){
                if (TextUtils.isEmpty(result)){
                    result = result + node.getId();
                } else {
                    result = result + ";" + node.getId();
                }
                selectCount ++;
                if (selectCount > 5){
                    return "error";
                }
            }
        }
        return result;
    }

}
